package com.jet.merchant.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jet.core.utils.IdUtil;
import com.jet.merchant.domain.dto.ClerkAccountRoleMapDto;
import com.jet.merchant.domain.entity.ClerkAccountRoleMap;
import com.jet.merchant.mapper.ClerkAccountRoleMapMapper;
import com.jet.merchant.service.IClerkAccountRoleMapService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-25
 */
@Service
public class ClerkAccountRoleMapServiceImpl extends ServiceImpl<ClerkAccountRoleMapMapper, ClerkAccountRoleMap> implements IClerkAccountRoleMapService {

    @Override
    public void save(ClerkAccountRoleMapDto dto) {
        ClerkAccountRoleMap entity = new ClerkAccountRoleMap();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(IdUtil.generateLower());
        entity.setCreateTime(LocalDateTime.now());
        super.save(entity);
    }

    public ClerkAccountRoleMap findByAccountId(String accountId) {
        if (StrUtil.isBlank(accountId)) {
            return null;
        }
        Wrapper<ClerkAccountRoleMap> wrapper = new LambdaQueryWrapper<ClerkAccountRoleMap>()
                .eq(ClerkAccountRoleMap::getAccountId, accountId);
        return super.getOne(wrapper);
    }

    public List<ClerkAccountRoleMap> findAllByRoleId(String roleId) {
        if (StrUtil.isBlank(roleId)) {
            return Collections.emptyList();
        }
        Wrapper<ClerkAccountRoleMap> wrapper = new LambdaQueryWrapper<ClerkAccountRoleMap>()
                .eq(ClerkAccountRoleMap::getRoleId, roleId);
        return super.list(wrapper);
    }

    public void deleteByAccountId(String accountId) {
        Wrapper<ClerkAccountRoleMap> wrapper = new LambdaQueryWrapper<ClerkAccountRoleMap>()
                .eq(ClerkAccountRoleMap::getAccountId, accountId);
        super.remove(wrapper);
    }
}
